<template>
  <el-dialog
    title="编辑员工"
    :visible="dialogVisible"
    center
    width="40%"
    @close="closeFn"
  >
    <!-- 新增员工信息 -->
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" style="width:50%" placeholder="请输入邮箱" />
      </el-form-item>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <ImageUpload ref="imageUpload" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="closeFn">取消</el-button>
          <el-button type="primary" size="small" @click="comfirmFn">提交</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>

</template>

<script>
import ImageUpload from './ImageUpload/index.vue'
import { getRoleList, getEmployeesById, editEmpolyee } from '@/api/employees'
export default {
  components: {
    ImageUpload
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {

      roleList: [],
      formData: {
        username: '',
        email: ''
      },
      rules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { pattern: /^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/, message: '邮箱不符合规范', trigger: 'blur' }

        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 3, max: 12, message: '密码长度3-12位', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    this.roleList = await getRoleList({
      _limit: 1000,
      _start: 0
    })
    // console.log(90, this.roleList)
  },
  methods: {
    // 根据id获取员工个人信息
    async getEmployeesById(id) {
      this.formData = await getEmployeesById(id)
      this.$refs.imageUpload.fileList.push({
        url: this.formData.image
      })
      console.log(100, this.formData)
    },
    // 点击确认，通过校验，获取编辑接口，提示信息，通知父组件调用，关闭弹框
    async comfirmFn() {
      this.$refs.form.validate()
      // 当图片没有上传完成的时候不允许提交
      if (this.$refs.imageUpload.fileList[0] && !this.$refs.imageUpload.fileList[0].url) {
        this.$message.error('图片未上传完成，请稍后！')
        return
      }
      // 当用户上传头像时，需要把上传头像的地址，赋值给表单数据，然后再提交
      this.formData.image = this.$refs.imageUpload.fileList[0] && this.$refs.imageUpload.fileList[0].url

      await editEmpolyee(this.formData.id, {
        ...this.formData
      })
      this.$message.success('编辑员工成功')
      this.$emit('editEmployee')
      this.closeFn()
    },
    // 关闭弹框
    closeFn() {
      this.$emit('update:dialogVisible', false)
      this.$refs.imageUpload.fileList = []
      this.formData = {
        username: '',
        email: ''
      }

      //   重置验证规则
      this.$refs.form.resetField()
    }
  }

}
</script>

<style lang="scss" >

.el-dialog__header {
    background: #66b1ff;

    .el-dialog__title{
       color: #fff;
    }

}
.el-dialog__body{
    margin-left: 100px;
}
h4{
  margin-left: 44px;
}
</style>
